Shader "Unlit/Glass"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _CubeMap ("CubeMap", Cube) = "white" {}
        //法线贴图
        _BumpMap ("BumpMap",2D) = ""{}
        _TransparentNum("TransparentNum", Range(0,1)) = 0.5
    }
    SubShader
    {
        GrabPass {}
        Tags
        {
            "RenderType"="Opaque"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fog

            #include "UnityCG.cginc"
            
            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 uv : TEXCOORD0;
                //从裁剪空间转换到屏幕空间的坐标，顶点相对于屏幕坐标的颜色
                float4 screenPos : TEXCOORD1;
                float3 wPos : TEXCOORD2;
                fixed3x3 turnMatrix:TEXCOORD3;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _GrabTexture;
            samplerCUBE _CubeMap;
            sampler2D _BumpMap;
            float4 _BumpMap_ST;
            float _TransparentNum;

            v2f vert(appdata_full v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.screenPos = ComputeGrabScreenPos(o.vertex);
                o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);

                //模型空间到世界空间
                float3 wTangentDir = UnityObjectToWorldDir(v.tangent);
                float3 wNormalDir = UnityObjectToWorldDir(v.normal);
                float3 binDir = cross(wNormalDir, wTangentDir);

                o.turnMatrix = float3x3(
                    wTangentDir.x, binDir.x, wNormalDir.x,
                    wTangentDir.y, binDir.y, wNormalDir.y,
                    wTangentDir.z, binDir.z, wNormalDir.z
                );
                
                // 使用位置而不是方向来计算折射方向
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                // float3 cameraToObjDir = normalize(worldPos - _WorldSpaceCameraPos);
                o.wPos = worldPos;
                // float3 reflactDir = reflect(cameraToObjDir, wNormal);
                // o.reflactDir = reflactDir;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                //主颜色
                fixed4 mainTex = tex2D(_MainTex, i.uv);

                //法线贴图采样
                float3 normal = mul(i.turnMatrix, UnpackNormal(tex2D(_BumpMap, i.uv.zw)));
                //计算相机方向向量
                float3 cameraToObjDir = normalize(UnityWorldSpaceViewDir(i.wPos));
                float3 reflactDir = reflect(-cameraToObjDir, normal);

                //tex2Dproj 用于从屏幕空间纹理中采样颜色
                //得到从屏幕采样的颜色
                //UNITY_PROJ_COORD 宏确保正确处理齐次坐标以进行投影纹理采样。
                fixed4 grabColor = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.screenPos));
                
                // 反射CubeMap环境颜色
                fixed4 refractAndCubeColor = texCUBE(_CubeMap, reflactDir) * mainTex;

                // 返回颜色 不透明->透明
                fixed4 endColor = lerp(grabColor, refractAndCubeColor, _TransparentNum);
                return endColor;
            }
            ENDCG
        }
    }
}
